package com.hyl.springbootappinfo.controller;

import com.hyl.springbootappinfo.pojo.Dev_user;
import com.hyl.springbootappinfo.service.Dev_userService;
import com.hyl.springbootappinfo.web.tools.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private Dev_userService dev_userService;


    @RequestMapping(value = {"/","/index",""})
    public String getindex(){
        return "index";
    }

    @RequestMapping("/dev/login")
    public String todevlogin(){
        return "devlogin";
    }

    @RequestMapping("/manager/login")
    public String tomanagerlogin(){
        return "backendlogin";
    }

    @RequestMapping(value = "/devdologin.html",method = RequestMethod.POST)
    public String todevdologin(Dev_user user, HttpSession session, HttpServletRequest request){
        Dev_user dev_user=dev_userService.login(user);
        if (dev_user!=null){
            session.setAttribute(Constants.USER_SESSION,dev_user);
            return "redirect:/from";
        }
        String error="账号或密码不正确";
        request.setAttribute("error",error);
        return "devlogin";
    }

    @RequestMapping("/from")
    public String togetdevlogin(){
        return "developer/main";
    }

    @RequestMapping("/devlogout")
    public String todevlogout(HttpServletRequest request){
        Dev_user user= (Dev_user) request.getSession().getAttribute(Constants.USER_SESSION);
        if (user!=null){
            request.getSession().removeAttribute(Constants.USER_SESSION);
        }
        return "redirect:/dev/login";
    }

}
